#ifndef CLNSPACE_CONNECTION_HPP
#define CLNSPACE_CONNECTION_HPP

#include "StdAfx.h"
#include "cln_handler.h"

namespace fly {

	namespace jld{

		namespace client
		{
			class connection_manager;

			class connection: public boost::enable_shared_from_this<connection>,
				private boost::noncopyable
			{
			public:
				explicit connection(boost::asio::io_service& io_service,
					connection_manager& manager, cln_handler* handler);

				/// Get the socket associated with the connection.
				boost::asio::ip::tcp::socket& socket();

				/// Start the first asynchronous operation for the connection.
				void start();

				/// Stop all asynchronous operations associated with the connection.
				void stop();

				cln_info& info();

				~connection();
			private:
				void read_header();
				void handle_header_read(const boost::system::error_code& err,std::size_t bytes_transferred);

				void read_body();
				void handle_body_read(const boost::system::error_code& err,std::size_t bytes_transferred);

				void asyn_write();
				void handle_write(const boost::system::error_code& err,std::size_t bytes_transferred);

				

				/// Socket for the connection.
				boost::asio::ip::tcp::socket socket_;

				/// The manager for this connection.
				connection_manager& connection_manager_;

				recv_buf recv_buf_;

				send_buf send_buf_;

				cln_info info_;

				cln_handler* handler_;


			};

			typedef boost::shared_ptr<connection> connection_ptr;//smart pointer
		}
	}
}

#endif